

#include "cybertron/network/MessageRouter.hpp"

CYBERTRON_BEGIN

CybertronCoreAPI MessageRouter::MessageRouter()
{
	zeroMembers();
}

CybertronCoreAPI MessageRouter::~MessageRouter()
{
	clear();
}

void MessageRouter::zeroMembers()
{
	// do nothing.
}

CybertronCoreAPI void MessageRouter::clear()
{
	mHandlers.clear();

	zeroMembers();
}

CybertronCoreAPI void MessageRouter::registerHandler(
	std::uint16_t msgId,
	std::function<void(Message&)> handler)
{
	if (handler == nullptr)
	{
		return;
	}

	mHandlers[msgId] = handler;
}

CybertronCoreAPI void MessageRouter::onMessage(Message& msg)
{
	std::uint16_t msgId = msg.parseMsgId();
	auto it = mHandlers.find(msgId);
	if (it == mHandlers.end())
	{
		return;
	}

	((*it).second)(msg);
}

CYBERTRON_END
